(function(){
function Rmcmd(){
var SALTCMD = {
    cmd: {
        run: 1,
    },
    cron: {
        
    },
    grains: {
        ls: 0,
        item: 1,
        items: 0
    },
    sys: {
        list_modules: 0,
        list_functions: 1,
    },
    proc: {
        detect: 1
    }
};

var breadcrumb = new Breadcurmb({
    prefix: [
        {text: '任务管理', anchor: '/task'}
    ],
    curr: '远程命令'
});

var MainView = Backbone.View.extend({
    // el与initialize一样是bk的关键字, 不能重命名为其它
    el: '#page-cnt',
    template:  _.template($("#tpl-rmcmd").html()),
    events: {
        'click .btn-execute': 'execute',
        'click .btn-clear': 'clear',
        'change select[name="mod"]': 'refreshFun',
        'change select[name="fun"]': 'refreshArg',
    },
    initialize: function() {
        // 创建面包屑视图
        this.breadcrumbView = new BreadcrumbView({
            model: breadcrumb
        });
        console.log('here init...');
        this.render();
    },
    render: function(){
        this.$el.html(this.template());
        this.opts = [
            '<option value="-1">模块</option>',
            '<option value="-1">选项</option>',
        ];
        console.log(this.opts);
        $('select[name="mod"]').append(this.opts[0]).select2();
        $('select[name="fun"]').append(this.opts[1]).select2();
        this.renderMod();
    },
    renderMod: function(e){
        /*
         * 模块部分一般是不会变的, 只有fun和arg才有可能会
         */
        var optNodes = [];
        for(var mod in SALTCMD){
            optNodes.push('<option value="' + mod + '">' + mod + '</option>');
        }
        console.log(optNodes);
        $('select[name="mod"]').append(optNodes).select2();
    },
    refreshFun: function(e){
        $('select[name="fun"]').empty();
        var mod = $('select[name="mod"]').val();
        var optNodes = [
            this.opts[1]
        ];
        for(var fun in SALTCMD[mod]){
            optNodes.push('<option value="' + fun + '">' + fun + '</option>');
        }
        $('select[name="fun"]').append(optNodes).select2();
    },
    refreshArg: function(e){
        $('input[name="arg"]').val('');
        var mod = $('select[name="mod"]').val();    
        var fun = $('select[name="fun"]').val();
        if(!SALTCMD[mod][fun])
            $('input[name="arg"]').attr('readonly', 'true');
        else
            $('input[name="arg"]').removeAttr('readonly');
    },
    clear: function(e){
        $('#rmcmd').find('.rmcmd-result-container').html('');
    },
    execute: function(e){
        var self = this;
        self.clear();
        var data = {
            'fun': $('select[name="mod"]').val() + '.' + $('select[name="fun"]').val(),
            'tgt': $('input[name="tgt"]').val(),
        };
        if(!$('input[name="arg"]').attr('readonly'))
            data.arg = $('input[name="arg"]').val()

        $.ajax({
            type: 'GET',
            url: apiList.execute,
            data: data,
            success: function(result){
                console.log(result);
                result = result.return[0];
                self.renderResult(result);
            }
        });
    },
    renderResult: function(result){
        var succNum = failNum = 0;
        for(i in result){
            if(result[i] == false){
                failNum ++;
            }else{
                succNum ++;

            }
        }

        var panels = [];
        var rmcmdResultMsg = $('.rmcmd-result-msg-example')
                                .clone()
                                .removeClass('rmcmd-result-msg-example hidden')
                                .addClass('rmcmd-result-msg');
        rmcmdResultMsg.find('.succ-num').text(succNum);
        rmcmdResultMsg.find('.fail-num').text(failNum);
        panels.push(rmcmdResultMsg);
        for(i in result){
            var rmcmdResultPanel = $('.rmcmd-result-panel-example')
                                    .clone()
                                    .removeClass('rmcmd-result-panel-example hidden')
                                    .addClass('rmcmd-result-panel');
            rmcmdResultPanel.find('.minion-id').text(i);
            if(typeof(result[i]) == 'object'){
                rmcmdResultPanel.find('.rmcmd-result-json').removeClass('hidden').JSONView(result[i], {collapsed: true});
            }else{
                rmcmdResultPanel.find('.rmcmd-result').removeClass('hidden');
                rmcmdResultPanel.find('.rmcmd-result code').text(result[i]).each(function(i, block) {
                    console.log(block);
                    console.log(typeof(block));
                    // 这里block的类型还需要再次确认<???>
                    hljs.highlightBlock(block);
                });
            }
            panels.push(rmcmdResultPanel);
        }
        $('.rmcmd-result-container').html(panels);
    }
});

var mainView = new MainView();
console.log('rmcmd start');

}

app.modules.Rmcmd = Rmcmd;

})();